package com.zhilianbao.leyaogo.view.dialog;

import android.content.Context;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bql.utils.StringUtils;
import com.bql.variousdialog.widget.base.BaseDialog;
import com.zhilianbao.leyaogo.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * ClassName: TipsDialog <br>
 * Description: Toast dialog<br>
 * Author: Cyarie <br>
 * Created: 2016/11/25 09:21 <br>
 * Update Time：<br>
 * Update Description：<br>
 */
public class ToastDialog extends BaseDialog<ToastDialog> {


    @BindView(R.id.iv_tips)
    ImageView mIvTips;
    @BindView(R.id.tv_tips)
    TextView mTvTips;

    private String tips;//文字内容

    private boolean isSuccess = true;//是否成功

    private int imageRes;//图片资源

    public ToastDialog(Context context) {
        super(context);
    }

    @Override
    public View onCreateView() {
        widthScale(0f);
        View view = View.inflate(mContext, R.layout.dialog_toast, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void setUiBeforeShow() {
        getWindow().setDimAmount(0.2f);
        setCanceledOnTouchOutside(false);
        mTvTips.setText(StringUtils.isEmpty(tips, mContext.getString(R.string.loading)));
        if (imageRes != 0) {
            mIvTips.setImageResource(imageRes);
        } else {
            mIvTips.setImageResource(isSuccess ? R.drawable.icon_success : R.drawable.icon_fail);
        }

    }

    public ToastDialog tips(String tips) {
        this.tips = tips;
        return this;
    }

    public ToastDialog tips(@StringRes int resId) {
        this.tips = mContext.getString(resId);
        return this;
    }

    public ToastDialog success() {
        this.isSuccess = true;
        return this;
    }

    public ToastDialog fail() {
        this.isSuccess = false;
        return this;
    }

    public ToastDialog imageRes(@DrawableRes int resId) {
        this.imageRes = resId;
        return this;
    }

    //重写系统返回键处理
    @Override
    public void onBackPressed() {
        if (isShowing()) {
            return;
        }
        super.onBackPressed();
    }
}
